package com.spring.mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.spring.mvc.bean.Cliente;
import com.spring.mvc.service.ClienteService;
import com.spring.mvc.util.Constantes;


@Controller
public class ClienteController {
	
	@Autowired
	private ClienteService clienteService;
	
	
	@RequestMapping(value = "/listacliente.do", method = RequestMethod.POST)
	@ResponseBody
	public JTableJSONResponse<Map<String, Object>> listadoCliente(){
		 JTableJSONResponse<Map<String, Object>> jstr=new JTableJSONResponse<Map<String,Object>>();
		try {
			List<Map<String, Object>> listaCliente = clienteService.selectAllCliente("",null,null);
		   jstr = new JTableJSONResponse<Map<String, Object>>("OK",listaCliente,listaCliente.size());
	
		} catch (Exception e) {
			e.printStackTrace();
		}
			    
	    return jstr;
	}
	
	@RequestMapping(value = "/listadoClienteByFilter.do", method = RequestMethod.POST)
	@ResponseBody
	public JTableJSONResponse<Map<String, Object>> listadoClienteByFilter(String nomCliente,String nroDoc){
		 JTableJSONResponse<Map<String, Object>> jstr=new JTableJSONResponse<Map<String,Object>>();
		try {
			List<Map<String, Object>> listaCliente = clienteService.selectAllCliente(nomCliente,null,nroDoc);
		   jstr = new JTableJSONResponse<Map<String, Object>>("OK",listaCliente,listaCliente.size());
	
		} catch (Exception e) {
			e.printStackTrace();
		}			    
	    return jstr;
	}
	
	@RequestMapping(value = "/registrarcliente.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> registrarCliente(Cliente cliente, HttpSession session) 
	throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();
		try{
			
		cliente.setDesUsuarioRegistro(session.getAttribute("desUsuarioLogin").toString());
		if(cliente.getIdCliente()==null){
			clienteService.insertCliente(cliente);
		}
		
		data.put("Record", cliente);
		data.put("Result", "OK");
		data.put("message", "Se registro satisfacoriamente.");
		}catch (Exception e) {
		e.printStackTrace();	
		//data.put("data", data);
		data.put("Result", "ERROR");
		data.put("message", "En estos momentos estamos en mantenimiento.");
		}
		return data;
	}

	
	@RequestMapping(value = "/actualizarcliente.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> actualizarCliente(Cliente cliente, HttpSession session)
	throws Exception {
		
		Map<String, Object> data = new HashMap<String, Object>();
		try{
		cliente.setDesUsuarioRegistro(session.getAttribute("desUsuarioLogin").toString());
		cliente = clienteService.updatebyIdCliente(cliente);
		data.put("Record", cliente);
		data.put("Result", "OK");
		data.put("message", "Se actualizo satisfacoriamente.");
		}catch (Exception e) {
		e.printStackTrace();	
		data.put("data", data);
		data.put("success", Boolean.FALSE);
		data.put("message", "En estos momentos estamos en mantenimiento.");
		}
		return data;
	}
	
	
	@RequestMapping(value = "/eliminarcliente.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> eliminarCliente(Integer idCliente)
	throws Exception {
		
		Map<String, Object> data = new HashMap<String, Object>();
		try{
		Cliente	cliente= new Cliente(); 
		cliente.setIdCliente(idCliente);
		cliente.setStCliente(Constantes.ESTADO_INACTIVO+"");
		cliente = clienteService.updatebyIdCliente(cliente);
		data.put("Result", "OK");
		data.put("message", "Se actualizo satisfacoriamente.");
		}catch (Exception e) {
		e.printStackTrace();	
		data.put("data", data);
		data.put("Result", "ERROR");
		data.put("message", "En estos momentos estamos en mantenimiento.");
		}
		return data;
	}
	
	

}
